/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package svp;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import svp.exceptions.IllegalOrphanException;
import svp.exceptions.NonexistentEntityException;
import svp.exceptions.PreexistingEntityException;

/**
 *
 * @author kandamaran
 */
public class ProductColorTblJpaController implements Serializable {

    public ProductColorTblJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ProductColorTbl productColorTbl) throws PreexistingEntityException, Exception {
        if (productColorTbl.getProductUnitTblCollection() == null) {
            productColorTbl.setProductUnitTblCollection(new ArrayList<ProductUnitTbl>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<ProductUnitTbl> attachedProductUnitTblCollection = new ArrayList<ProductUnitTbl>();
            for (ProductUnitTbl productUnitTblCollectionProductUnitTblToAttach : productColorTbl.getProductUnitTblCollection()) {
                productUnitTblCollectionProductUnitTblToAttach = em.getReference(productUnitTblCollectionProductUnitTblToAttach.getClass(), productUnitTblCollectionProductUnitTblToAttach.getIproductunitID());
                attachedProductUnitTblCollection.add(productUnitTblCollectionProductUnitTblToAttach);
            }
            productColorTbl.setProductUnitTblCollection(attachedProductUnitTblCollection);
            em.persist(productColorTbl);
            for (ProductUnitTbl productUnitTblCollectionProductUnitTbl : productColorTbl.getProductUnitTblCollection()) {
                ProductColorTbl oldProductColorOfProductUnitTblCollectionProductUnitTbl = productUnitTblCollectionProductUnitTbl.getProductColor();
                productUnitTblCollectionProductUnitTbl.setProductColor(productColorTbl);
                productUnitTblCollectionProductUnitTbl = em.merge(productUnitTblCollectionProductUnitTbl);
                if (oldProductColorOfProductUnitTblCollectionProductUnitTbl != null) {
                    oldProductColorOfProductUnitTblCollectionProductUnitTbl.getProductUnitTblCollection().remove(productUnitTblCollectionProductUnitTbl);
                    oldProductColorOfProductUnitTblCollectionProductUnitTbl = em.merge(oldProductColorOfProductUnitTblCollectionProductUnitTbl);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findProductColorTbl(productColorTbl.getProductColor()) != null) {
                throw new PreexistingEntityException("ProductColorTbl " + productColorTbl + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ProductColorTbl productColorTbl) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProductColorTbl persistentProductColorTbl = em.find(ProductColorTbl.class, productColorTbl.getProductColor());
            Collection<ProductUnitTbl> productUnitTblCollectionOld = persistentProductColorTbl.getProductUnitTblCollection();
            Collection<ProductUnitTbl> productUnitTblCollectionNew = productColorTbl.getProductUnitTblCollection();
            List<String> illegalOrphanMessages = null;
            for (ProductUnitTbl productUnitTblCollectionOldProductUnitTbl : productUnitTblCollectionOld) {
                if (!productUnitTblCollectionNew.contains(productUnitTblCollectionOldProductUnitTbl)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain ProductUnitTbl " + productUnitTblCollectionOldProductUnitTbl + " since its productColor field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<ProductUnitTbl> attachedProductUnitTblCollectionNew = new ArrayList<ProductUnitTbl>();
            for (ProductUnitTbl productUnitTblCollectionNewProductUnitTblToAttach : productUnitTblCollectionNew) {
                productUnitTblCollectionNewProductUnitTblToAttach = em.getReference(productUnitTblCollectionNewProductUnitTblToAttach.getClass(), productUnitTblCollectionNewProductUnitTblToAttach.getIproductunitID());
                attachedProductUnitTblCollectionNew.add(productUnitTblCollectionNewProductUnitTblToAttach);
            }
            productUnitTblCollectionNew = attachedProductUnitTblCollectionNew;
            productColorTbl.setProductUnitTblCollection(productUnitTblCollectionNew);
            productColorTbl = em.merge(productColorTbl);
            for (ProductUnitTbl productUnitTblCollectionNewProductUnitTbl : productUnitTblCollectionNew) {
                if (!productUnitTblCollectionOld.contains(productUnitTblCollectionNewProductUnitTbl)) {
                    ProductColorTbl oldProductColorOfProductUnitTblCollectionNewProductUnitTbl = productUnitTblCollectionNewProductUnitTbl.getProductColor();
                    productUnitTblCollectionNewProductUnitTbl.setProductColor(productColorTbl);
                    productUnitTblCollectionNewProductUnitTbl = em.merge(productUnitTblCollectionNewProductUnitTbl);
                    if (oldProductColorOfProductUnitTblCollectionNewProductUnitTbl != null && !oldProductColorOfProductUnitTblCollectionNewProductUnitTbl.equals(productColorTbl)) {
                        oldProductColorOfProductUnitTblCollectionNewProductUnitTbl.getProductUnitTblCollection().remove(productUnitTblCollectionNewProductUnitTbl);
                        oldProductColorOfProductUnitTblCollectionNewProductUnitTbl = em.merge(oldProductColorOfProductUnitTblCollectionNewProductUnitTbl);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = productColorTbl.getProductColor();
                if (findProductColorTbl(id) == null) {
                    throw new NonexistentEntityException("The productColorTbl with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProductColorTbl productColorTbl;
            try {
                productColorTbl = em.getReference(ProductColorTbl.class, id);
                productColorTbl.getProductColor();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The productColorTbl with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<ProductUnitTbl> productUnitTblCollectionOrphanCheck = productColorTbl.getProductUnitTblCollection();
            for (ProductUnitTbl productUnitTblCollectionOrphanCheckProductUnitTbl : productUnitTblCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This ProductColorTbl (" + productColorTbl + ") cannot be destroyed since the ProductUnitTbl " + productUnitTblCollectionOrphanCheckProductUnitTbl + " in its productUnitTblCollection field has a non-nullable productColor field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(productColorTbl);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ProductColorTbl> findProductColorTblEntities() {
        return findProductColorTblEntities(true, -1, -1);
    }

    public List<ProductColorTbl> findProductColorTblEntities(int maxResults, int firstResult) {
        return findProductColorTblEntities(false, maxResults, firstResult);
    }

    private List<ProductColorTbl> findProductColorTblEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ProductColorTbl.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ProductColorTbl findProductColorTbl(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ProductColorTbl.class, id);
        } finally {
            em.close();
        }
    }

    public int getProductColorTblCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ProductColorTbl> rt = cq.from(ProductColorTbl.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
